package com.mstore.dao;

import java.util.List;

import com.mstore.pojo.EnterpriseIntegralRule;

public class EnterpriseIntegralRuleDao extends SuperDao {

	// 保存积分规则
	public void saveEnterpriseIntegralRule(
			EnterpriseIntegralRule enterpriseIntegralRule) {
		super.save(enterpriseIntegralRule);
	}

	// 分页查看积分规则
	public List<Object> watchEnterpriseIntegralRulePage(int index,
			EnterpriseIntegralRule enterpriseIntegralRule) {
		/*
		 * String hql = "FROM EnterpriseIntegralRule WHERE eaId = " +
		 * enterpriseIntegralRule.getEaId(); return super.findPage(index, hql);
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("FROM EnterpriseIntegralRule WHERE eaId =");
		sb.append(enterpriseIntegralRule.getEaId());
		return super.findPage(index, sb.toString());
	}

	// 查看积分规则
	public List<Object> watchEnterpriseIntegralRule(
			EnterpriseIntegralRule enterpriseIntegralRule) {
		String hql = "FROM EnterpriseIntegralRule WHERE eaId = ?";
		Object[] values = new Object[] { enterpriseIntegralRule.getEaId() };
		return super.getObject(hql, values);
	}

	// 获取一个积分规则对象
	public Object getOneEnterpriseIntegralRule(
			EnterpriseIntegralRule enterpriseIntegralRule) {
		String hql = "FROM EnterpriseIntegralRule WHERE id = ?";
		Object[] values = new Object[] { enterpriseIntegralRule.getId() };
		List<Object> list = super.getObject(hql, values);
		if (list.size() != 0) {
			return list.get(0);
		}
		return null;
	}

	// 修改积分规则
	public void modifyEnterpriseIntegralRule(
			EnterpriseIntegralRule enterpriseIntegralRule) {
		/*
		 * String sql = "UPDATE ubcenter.b_integral_rule SET name = '" +
		 * enterpriseIntegralRule.getName() + "', b_minValue = " +
		 * enterpriseIntegralRule.getMinValue() + ", b_maxValue = " +
		 * enterpriseIntegralRule.getMaxValue() + ", descirption = '" +
		 * enterpriseIntegralRule.getDescirption() + "' WHERE id = " +
		 * enterpriseIntegralRule.getId(); super.executeUpdate(sql);
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("UPDATE ubcenter.b_integral_rule SET name = '");
		sb.append(enterpriseIntegralRule.getName());
		sb.append("', b_minValue = ");
		sb.append(enterpriseIntegralRule.getMinValue());
		sb.append(", b_maxValue = ");
		sb.append(enterpriseIntegralRule.getMaxValue());
		sb.append(", descirption = '");
		sb.append(enterpriseIntegralRule.getDescirption());
		sb.append("' WHERE id = ");
		sb.append(enterpriseIntegralRule.getId());
		super.executeUpdate(sb.toString());
	}

	// 删除积分规则
	public void delEnterpriseIntegralRule(
			EnterpriseIntegralRule enterpriseIntegralRule) {
		super.delete(enterpriseIntegralRule);
	}

	// 查询其余积分规则
	public List<Object> getEnterpriseIntegralRuleWithoutSomeId(
			EnterpriseIntegralRule enterpriseIntegralRule) {
		String hql = "FROM EnterpriseIntegralRule WHERE eaId = ? and id != ?";
		Object[] values = new Object[] { enterpriseIntegralRule.getEaId(),
				enterpriseIntegralRule.getId() };
		return super.getObject(hql, values);
	}
	
	/**
	 * 根据积分区间返回积分规则
	 */
	public Object getEnterpriseIntegralRuleByValues(Long eaId, int value) {
		String hql = "FROM EnterpriseIntegralRule WHERE eaId = ? and (? BETWEEN minValue and maxValue)";
		List<Object> list = getObject(hql, new Object[] { eaId, value });
		return list.isEmpty() ? null : list.get(0);
	}
	
}
